package com.zj.controller;

import com.zj.pojo.Apple;
import com.zj.pojo.Result;
import com.zj.services.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/apple")
public class AppleController {
    private static Logger log = LoggerFactory.getLogger(AppleController.class);

    @Autowired
    private BaseService service;


    @GetMapping("/{id}")
    public Result getApple(@PathVariable int id) {
        log.info("请求getApple进入" + id);
        Apple apple = service.getAppleById(id);
        return new Result(HttpStatus.OK.value(), "query success", apple);
    }

    @PostMapping("/add")
    public Result addApple(@RequestBody @Validated Apple apple) {
        log.info("请求addApple进入" + apple.toString());
        int size = service.insertApples(apple);
        return new Result<>(HttpStatus.OK.value(), "insert success", size);
    }

    @PutMapping("/{id}")
    public Result updateApple(@PathVariable int id, @RequestBody @Validated Apple apple) {
        log.info("请求updateApple进入" + apple.toString());

        List<Apple> apppool = service.updateAppleById(id, apple);
        return new Result<>(HttpStatus.OK.value(), "update success", apppool);
    }

    @GetMapping("/all")
    public Result getAll() {
        log.info("请求getAll进入");

        List<Apple> apppool = service.getApppool();
        return new Result<>(HttpStatus.OK.value(), "query all success", apppool);
    }

    @DeleteMapping("/{id}")
    public Result deleteApple(@PathVariable int id) {
        log.info("请求deleteApple进入" + id);

        List<Apple> apppool = service.deleteApple(id);
        return new Result<>(HttpStatus.OK.value(), "delete success", apppool);

    }
}
